package com.sh.data.engine.domain.shims.sql.select;

import com.sh.data.engine.domain.shims.sql.Context;
import com.sh.data.engine.domain.shims.sql.IllegalQueryException;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

abstract class Condition {

    protected final Context context;

    Condition(Context context) {
        this.context = context;
    }

    void add(Object condition) {
        context.appendWithSeparator(getPrefix() + " " + condition);
    }

    void add(Object condition, Object parameter) {
        if (parameter != null) {
            add(condition, new Object[]{parameter});
        }
    }

    void add(String condition, String parameter) {
        if (StringUtils.isNotBlank(parameter)) {
            add(condition, new Object[]{parameter});
        }
    }

    void add(Object condition, Object... parameters) {
        if (ArrayUtils.isNotEmpty(parameters)) {
            context.addParameters(parameters);
            add(condition);
        }
    }

    void between(String columnName, Object start, Object end) {
        if (start == null) {
            if (end != null) {
                add(columnName + " <= ?", end);
            }
        } else {
            if (end == null) {
                add(columnName + " >= ?", start);
            } else {
                add(columnName + " BETWEEN ? AND ?", start, end);
            }
        }
    }

    void in(String columnName, List<Object> parameters) {
        if (null == parameters || parameters.size() == 0) {
            throw new IllegalQueryException("in条件的参数为空");
        }

        StringBuilder inBuilder = new StringBuilder(" IN (");

        for (int i = 0; i < parameters.size(); i++) {
            if (i == parameters.size() - 1) {
                inBuilder.append("?");
            } else {
                inBuilder.append("?,");
            }
        }

        inBuilder.append(")");

        add(columnName + inBuilder.toString(), parameters);
    }

    void notIn(String columnName, List<Object> parameters) {
        if (null == parameters || parameters.size() == 0) {
            throw new IllegalQueryException("in条件的参数为空");
        }

        StringBuilder inBuilder = new StringBuilder(" NOT IN (");

        for (int i = 0; i < parameters.size(); i++) {
            if (i == parameters.size() - 1) {
                inBuilder.append("?");
            } else {
                inBuilder.append("?,");
            }
        }

        inBuilder.append(")");

        add(columnName + inBuilder.toString(), parameters);
    }

    void in(String columnName, Object... parameters) {
        if (null == parameters || parameters.length == 0) {
            throw new IllegalQueryException("in条件的参数为空");
        }

        StringBuilder inBuilder = new StringBuilder(" IN (");

        for (int i = 0; i < parameters.length; i++) {
            if (i == parameters.length - 1) {
                inBuilder.append("?");
            } else {
                inBuilder.append("?,");
            }
        }

        inBuilder.append(")");

        add(columnName + inBuilder.toString(), parameters);
    }

    void notIn(String columnName, Object... parameters) {
        if (null == parameters || parameters.length == 0) {
            throw new IllegalQueryException("in条件的参数为空");
        }

        StringBuilder inBuilder = new StringBuilder(" NOT IN (");

        for (int i = 0; i < parameters.length; i++) {
            if (i == parameters.length - 1) {
                inBuilder.append("?");
            } else {
                inBuilder.append("?,");
            }
        }

        inBuilder.append(")");

        add(columnName + inBuilder.toString(), parameters);
    }

    protected abstract String getPrefix();
}
